<?php
namespace app\index\controller;
use think\Controller;
use think\Collection;
use think\Db;
class Index extends CommonController
{
    public function index()
    { 
       // return   dump(checkSign());
            return $this->fetch();
        
    }

//时间轴
    public function timetable(){
        //date.ToString("yyyy-MM-dd")    
        //  $years_2022 = Db::table('time_table')->where("time like '2018%'")->order('time desc')->select();
        //  $this->assign('list',$years_2022);
        //  $years_2015=Db::table('time_table')->where("time like '2018%'")->order('time desc')->select();
        //  $this->assign('list_1',$years_2015);
        $data = Db::table('time_table')->select();
            // var_dump($data1);
            return json($data);
        }
        public function time_table(){
            $data = Db::table('time_table')->select();
            $this -> assign('name',$data);
            // var_dump($this);
            // var_dump($data);
            return $this->fetch('index/timetable');
        }

//奥运宣传
    public function  propaganda(){
        $data=Db::query("SELECT * FROM propaganda order by id desc" );
        $this ->assign('propaganda',$data);
		//var_dump(json($this));
             return $this->fetch('index/propaganda');
    }

    public function  propagandaweb($id){
        $data=Db::query("SELECT * FROM propaganda  where id=$id order by id desc" );
        $this ->assign('list',$data);
        //$this ->assign('propaganda',$data);
		//var_dump(json($this));
        //return var_dump($data);
         return $this->fetch('index/propagandaweb');
    }


//天气
    public function weather(){
        // $handle = fopen("https://www.sojson.com/open/api/weather/json.shtml?city=北京","rb");
        // $content = "";
        // while (!feof($handle)){
        //     $content = fread($handle,10000);
        // }
        // fclose($handle);
        // var_dump($content);
        $url="https://www.sojson.com/open/api/weather/json.shtml?city=北京";
        // $ch = curl_init ();
        // curl_setopt($ch, CURLOPT_URL, $url);
        // curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        // curl_setopt($ch, CURLOPT_HEADER, 0);

        // $output=curl_exec($ch);

        // curl_close($ch);
        $content=file_get_contents($url);
        $data=json_decode($content,true);
        // var_dump($data[]);
        // var_dump($data.type);
        return $data;        
    }
    // 金牌
    public function gold_data(){
        $data = Db::table('gold')->select();
        
        // var_dump($data1);
        return json($data);
    }
    public function gold(){
        $data = Db::table('gold')->select();
        $this -> assign('name',$data);
        // var_dump($this);
        return $this->fetch('index/gold');
    }


    //发帖
    public function post(){
        return $this->fetch('common/post');
    }
    public function post_insert()
    {
        $db = Db::table('post_page');
        $page_title=input('title');
        if(request()->isPost()){
            
            $page_content = input('post.editor');
            $content= strip_tags($page_content);
            // var_dump($conters);
            
        }
        $time = date("Y-m-d H:i:s");
        $user_id=getUserId();//管理员身份
        // var_dump($user_id);
        // $imges=null;
        //正则得到图片的路径
        $pattern1= '/<img.*?src=[\"|\']?(.*?)[\"|\']?\s.*?>/i';
        // var_dump($pattern1);
         
        $m1=$m2=array();
        // var_dump($m1);
        preg_match_all($pattern1,$page_content,$m1);
        // var_dump( preg_match_all($pattern1,$page_content,$m1));
        if (preg_match_all($pattern1,$page_content,$m1)!=0) {
        //多个图片路径拼接成一个字符用 ，隔开
        //去掉末尾多余的
        $imges=$m1[1][0];
        }   
        else{
            $imges=null;
        }
        $insert=$db->insert(['page_title'=>$page_title,'page_content'=>$page_content,'page_time'=>$time,'user_id'=>$user_id,'page_image'=>$imges,'content'=>$content]);
        if($insert){
            $this->success('添加成功!','post');
        }else{
            $this->error('添加错误!','post');
        }
    }

}
